#pragma once

#include <QObject>
#include <QVector>
#include <QFile>
#include "LayoutObject.h"

class LayoutObject;
class Layout : public QObject
{
	Q_OBJECT
public:
	Layout(void);
	~Layout(void);
public:
	void AddObject(LayoutObject* obj);
	void RemoveObject(LayoutObject* obj);
	bool IsDirty() const { return isDirty_; }
	void Save(const QString& savePath) const
	{
		QFile file(savePath);
		file.open(QIODevice::WriteOnly);
		QDataStream out(&file);   // we will serialize the data into the file
		out << (int)objects_.count();

		for(int i = 0; i < objects_.count(); i++)
		{
			auto obj = objects_.at(i);
			obj->SerializeTo(out);
		}

		file.flush();
		file.close(); 
	}
	static Layout* Load(const QString& loadPath)
	{
		Layout* layout = new Layout();
		QFile file(loadPath);
		file.open(QIODevice::ReadOnly);
		QDataStream out(&file);   // we will serialize the data into the file
		int count;
		out >> count;

		for(int i = 0; i < count; i++)
		{
			auto obj = new LayoutObject();
			obj->DeserializeFrom(out);
			layout->AddObject(obj);
		}
		//match the prefab types
		auto prefabRepo = PrefabRepository::Instance();
		file.close(); 
		return layout;
	}
	const QVector<LayoutObject*>* GetAllObjects() const
	{
		return &objects_;
	}
public slots:
	void ItemChanged();
signals:
	void ObjectAdded(LayoutObject* obj);
	void ObjectRemoved(LayoutObject* obj);
private:
	QVector<LayoutObject*> objects_;
	bool isDirty_;
};

